 {% extends 'user/base.html' %}

{% block title %}
    <title>订单管理</title>
{% endblock %}

{% block content %}
<section class="slice pt-2 bg-surface-secondary">
    <div class="container mt-4 mb-5 pl-6  ">
        <div class="row">
            <div class="col col-md-3 mb-sm-4">
                <div class="col col-md12">
                    <div class="card-header pb-2 list-group-item-info">
                        个人信息
                    </div>
                    <ul class="list-group list-group-flush" id="myList">
                        <li class="list-group-item"><a href="{{ url_for('users.userInfo') }}" class="link-primary">个人资料</a></li>
                        <li class="list-group-item"><a href="{{ url_for('address.address_management') }}" class="link-primary">地址管理</a></li>
                        <li class="list-group-item" style="background-color: #cefaff;"><a href="{{ url_for('user.order_management') }}" class="link-primary">订单管理</a></li>
                        <li class="list-group-item">
                            <a href="{{ url_for('story.upload_story') }}" class="link-primary">上传故事管理</a>
                        </li>
                    </ul>
                    <div class="card-footer pb-2 list-group-item-info">
                        <p></p>
                    </div>
                </div>
            </div>
            <div class="col col-md-8 col-sm-12 ml-2 p-4 resume_box_body">
                <div class="container my-4">
                    <!-- 新增：Flash 消息显示区域 -->
                    <div class="flash-messages">
                        {% with messages = get_flashed_messages(with_categories=true) %}
                            {% if messages %}
                                {% for category, message in messages %}
                                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                                        {{ message }}
                                        <button type="button" class="close" data-bs-dismiss="alert" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                {% endfor %}
                            {% endif %}
                        {% endwith %}
                    </div>
                    <h3 class="mb-4">订单管理</h3>

                    {% if orders.items %}
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>订单编号</th>
                                    <th>下单时间</th>
                                    <th>状态</th>
                                    <th>收货地址</th>
                                    <th>操作</th>
                                    <th>商家状态</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for order in orders.items %}
                                <tr>
                                    <td>{{ order.id }}</td>
                                    <td>{{ order.order_time.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                                    <td>
                                        {% if order.status == 'pending' %}
                                            待处理
                                        {% elif order.status == 'paid' %}
                                            已支付
                                        {% elif order.status == 'unpaid' %}
                                            未支付
                                        {% else %}
                                            未知状态
                                        {% endif %}
                                    </td>
                                    <td>{{ order.address }}</td>
                                    <td>
                                        <a href="{{ url_for('user.order_detail', order_id=order.id) }}" class="btn btn-sm btn-info">查看</a>
                                        {% if order.status == 'unpaid' %}
                                            <form action="{{ url_for('user.delete_order', order_id=order.id) }}" method="POST" style="display:inline;">
                                                <button type="submit" onclick="return confirm('确定要删除吗？');" class="btn btn-sm btn-danger">删除</button>
                                            </form>
                                        {% endif %}
                                    </td>
                                    <td>{{order.admin_status}}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>

                        <!-- 分页控件 -->
                        <nav aria-label="Page navigation" class="text-center">
                            <ul class="pagination justify-content-center">
                                {% if orders.has_prev %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('user.order_management', page=orders.prev_num) }}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                                {% for page in orders.iter_pages() %}
                                    {% if page %}
                                    <li class="page-item {% if page == orders.page %}active{% endif %}">
                                        <a class="page-link" href="{{ url_for('user.order_management', page=page) }}">{{ page }}</a>
                                    </li>
                                    {% else %}
                                    <li class="page-item disabled"><span class="page-link">...</span></li>
                                    {% endif %}
                                {% endfor %}
                                {% if orders.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('user.order_management', page=orders.next_num) }}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                            </ul>
                        </nav>
                    {% else %}
                        <p class="text-center">您还没有任何订单。</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</section>
{% endblock %}